package cn.rongdao.jrkabao.ui.loan.success;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.statusbar.FRStatusBar;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.loan.ApplySuccessBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.loan.ApplySuccessInfoModel;
import cn.rongdao.jrkabao.ui.loan.uicomponent.ApplySuccessHeaderUIComponent;
import cn.rongdao.jrkabao.ui.loan.uicomponent.ApplySuccessListUIComponent;
import cn.rongdao.jrkabao.ui.main.MainActivity;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

/**
 * Created by jack on 17/5/18
 */

public class ApplySuccessActivity extends BaseAutoFinishActivity {

    @BindView(R.id.aas_titlebar)
    protected TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.aas_layout_apply_success)
    protected ApplySuccessHeaderUIComponent applySuccessHeaderUIComponent;
    @BindView(R.id.aas_list_apply)
    ApplySuccessListUIComponent applySuccessApply;
    @BindView(R.id.aas_list_more)
    ApplySuccessListUIComponent applySuccessMore;


    protected String orderId;
    private String mOrderDetailLink;


    public static void start(Context context, String cateId, String orderId) {
        Bundle bundle = new Bundle();
        bundle.putString(InfoConstant.CATE_ID, cateId);
        bundle.putString(InfoConstant.ORDER_ID, orderId);
        FRStartActivity.start(context, ApplySuccessActivity.class, bundle);
    }


    @Override
    protected void initView() {
        FRStatusBar.setTranslucentForImageView(this, findViewById(R.id.aas_rootview));
        orderId = getIntent().getStringExtra(InfoConstant.ORDER_ID);
        initTitleBar();
        getApplySuccessInfo();
    }

    private void getApplySuccessInfo() {
        ApplySuccessInfoModel.getInstance().execute(orderId, new FRBaseObserver<ApplySuccessBean>(this, "加载中，请稍后") {
            @Override
            protected void onBaseNext(ApplySuccessBean data) {
                mOrderDetailLink = data.getOrderDetailLink();
                parseData(data);
            }
        });
    }


    protected void parseData(ApplySuccessBean data) {
        applySuccessHeaderUIComponent.setContent("申请成功", data.getPage_msg().getBig_title(), data.getPage_msg().getSmall_title());
        if (data.getList_msg().size() == 1) {
            applySuccessMore.setAdapterDataList(data.getList_msg().get(0).getData_list());
            applySuccessMore.setContent(data.getList_msg().get(0).getTitle());
        } else if (data.getList_msg().size() == 2) {
            applySuccessApply.setAdapterDataList(data.getList_msg().get(0).getData_list());
            applySuccessApply.setContent(data.getList_msg().get(0).getTitle());
            applySuccessMore.setAdapterDataList(data.getList_msg().get(1).getData_list());
            applySuccessMore.setContent(data.getList_msg().get(1).getTitle());
        }
    }


    protected void initTitleBar() {
        titleBarUIComponent.initTitle("申请成功");
        titleBarUIComponent.initRight("查看详情", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startToMainAndShowPersonalFragment();
                if (TextUtils.isEmpty(mOrderDetailLink))
                    return;
                WebViewActivity.start(getActivity(), mOrderDetailLink);
            }
        });

        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startToMain();
            }
        });
    }

    protected void startToMainAndShowPersonalFragment() {
        MainActivity.Companion.start(thisActivity(), InfoConstant.FRAGMENT_TAG_PERSONAL);
        finish();
    }

    protected void startToMain() {
        MainActivity.Companion.start(thisActivity());
        finish();
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_apply_success;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startToMain();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}